<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>投票发起</title>
    <script src="../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js" charset="utf-8"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../res/images/favicon.ico">
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
</head>
<script>
    $.ajaxSetup({
        async: false
    });
</script>
<body style="padding: 10px">
<div style="display: none" id="bodys">
    <form action="" class="layui-form layui-form-pane" lay-filter="example">
        <h1 align="center" style="margin-bottom: 20px">投票</h1>
        <!--流程id-->
        <input name="pkVoteId" id="pkVoteId" type="hidden"/>

        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">投票标题</label>
                <div class="layui-input-block">
                    <input type="text" name="voteName" id="voteName" disabled lay-verify="required" autocomplete="off"
                           placeholder="请输入(必填)" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属分类</label>
            <div class="layui-input-block">
                <div id="voteTypeSuperior" class="xm-select-demo"></div>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">详细内容</label>
            <div class="layui-input-block">
            <textarea placeholder="请输入内容" class="layui-textarea" id="voteBody"
                      name="voteBody" lay-verify="required" disabled>
            </textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">截至时间</label>
                <div class="layui-input-inline">
                    <input type="text" disabled id="date" placeholder="yyyy-MM-dd" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>投票选项</legend>
                <div id="voteOption">
                </div>
            </fieldset>
            <input type="button" class="layui-btn layui-btn-normal site-demo-active" id="addVoteOption" value="投票"/>
        </div>

    </form>
</div>
<script>
    let pkVoteId = getQueryString("pkVoteId");
    let disabled = false;
    let vateDeadline;
    layui.use(['form', 'laydate', 'element'], function () {
        var form = layui.form
            , layer = layui.layer
            , element = layui.element;

        let voteData = getAjaxById(getRootPath() + "OaVoteAction/findById/" + pkVoteId);
        //投票标题
        $("#voteName").val(voteData.voteName);
        //投票内容
        $("#voteBody").val(voteData.voteBody);
        vateDeadline = new Date(voteData.vateDeadline);
        //截止时间
        $("#date").val(dateFormat("yyyy-MM-dd", vateDeadline));

        //设置50%进度
        /*element.progress('demo1', '50%');

        //设置50%进度
        element.progress('demo2', '50%');

        //设置50%进度
        element.progress('demo3', '50%');*/

        //投票提交
        $("#addVoteOption").on('click', function () {
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');

            let res = getAjaxById(getRootPath() + "OaVoteOptionAction/addVote/" + $("input[name=voteOptionId]:checked").val());
            if (res.code === 1) {
                layer.msg('投票成功3秒后关闭当前窗口', {icon: 1});
                setTimeout(function () {
                    formClose();
                }, 3000);
                setTimeout(function () {
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }, 3000);
                //window.location.reload();//刷新页面
            } else if (res.code === 500) {
                error('500');
            } else {
                layer.msg("新增失败");
            }

        });

        //加载组件
        layui.config({
            //引入扩展文件库
            base: getRootPath() + 'res/layui/dist/'
        }).extend({
            //引入对应的文件名
            xmSelect: 'xm-select'
            //拿到对应的文件模块
        }).use(['xmSelect'], function () {
            //对于模块赋值使用
            var xmSelect = layui.xmSelect;
            //渲染多选
            pkJurisdictionId = xmSelect.render({
                el: '#voteTypeSuperior',
                radio: true,
                disabled: disabled,
                name: 'voteTypeSuperior',
                disabled: true,
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [-1],
                },
                height: 'auto',
                data: loadVoteType(voteData.voteTypeId)
            });
        });

        let isVote = getAjaxById(getRootPath() + "OaVoteAction/isVote/" + pkVoteId);
        let voteOptionData = getAjaxById(getRootPath() + "OaVoteAction/voteOptionShow/" + pkVoteId);
        $("#voteOption").html("");
        if (isVote || vateDeadline<new Date()) {
            $("#addVoteOption").hide();
            $.each(voteOptionData, function (index, item) {
                $("#voteOption").append('<legend style="margin-top: 10px">' + item.voteOption.voteOptionName + '</legend><div class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="' + item.voteOption.pkVoteOptionId + '">\n' +
                    '                        <div class="layui-progress-bar" style="color: white">'+item.amount+'%</div>\n' +
                    '                    </div>');
                element.progress('' + item.voteOption.pkVoteOptionId, item.amount + '%');
            })
        } else {
            $.each(voteOptionData, function (index, item) {
                $("#voteOption").append('<div class="layui-input-block">\n' +
                    '                        <input type="radio" name="voteOptionId" value="' + item.voteOption.pkVoteOptionId + '" title="' + item.voteOption.voteOptionName + '" checked="">\n' +
                    '                    </div>');
            })
        }

        function loadVoteType(id) {
            let url = getRootPath() + "OaVoteTypeAction/loadVoteType";
            let data = {
                id: id,
                show: "2"
            };
            let res = getAjax(url, data);
            if (res) {
                return res;
            } else {
                return [];
            }
        }

        form.render();

        //监听起草提交
        /*form.on('submit(demo1)', function (data) {
            let ids = [];
            $("input[name=pkVoteOptionId]").each(function (index, item) {
                if (item.value) {
                    ids.push(item.value);
                }
            });
            data.field.pkVoteOptionId = ids;
            /!*data={
                affirmTime:$("#affirmTime").val(),
                personnelId:$("#personnelId").val()
            };*!/
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            const res = postAjax(getRootPath() + "OaWorkSummaryAction/add", data.field);
            if (res.code === 1) {
                layer.msg('新增成功3秒后关闭当前窗口', {icon: 1});
                setTimeout(function () {
                    formClose();
                }, 3000);
                setTimeout(function () {
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }, 3000);
                //window.location.reload();//刷新页面
            } else if (res.code === 500) {
                error('500');
            } else {
                layer.msg("新增失败");
            }
            return false;
        });*/

        $("#deleteById").on("click", function () {
            //询问框
            layer.confirm('是否取消投票', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                formClose();
                parent.layui.table.reload('test');//刷新父页面
                //window.parent.location.reload();//刷新父页面
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });

        });

        /**
         * 禁用
         *  @param forms 控件
         */
        function formDisabled(forms) {
            const DISABLED = 'layui-btn-disabled';
            forms.attr('disabled', 'disabled');
            forms.addClass(DISABLED);
        }


        $("#bodys").show();
        form.render();
    });
</script>
</body>
</html>